<?php
class Admin_SiteSettingController extends Apps_Controller_Action_Admin {
    public function indexAction() {
        $this->_forward('read');
    }

	public function readAction() {
		$rq   = $this->_request;

    	$tbSiteSetting = new Model_DbTable_SiteSetting();
        $siteSetting   = $tbSiteSetting->getSiteSetting()->query()->fetch();

        $form = new Admin_Form_AddSiteSetting();
        if($rq->isPost()) {
            if($form->isValid($rq->getParams())) {
                $data = $form->getValues();
                $data['password_email'] = rand(10000, 99999).'vct'.$form->getValue('password_email').'@760211'; //encode
                if(!$siteSetting) {
                    $r = $tbSiteSetting->safelyInsert($data);
                } else {
                    $r = $tbSiteSetting->safelyUpdate($data, array('id =?' => $siteSetting['id']));
                }
                $this->_flashMessenger->addMessage(
                	$r
                    ? self::$msgAddSuccess
                    : self::$msgAddFailed);
                return $this->goToActionIndex();
            }
        } else {
            if($siteSetting) {
                $passwordEmail = substr(substr($siteSetting['password_email'], 8), 0, -7);
                $siteSetting['password_email'] = $passwordEmail;

                $form->populate($siteSetting);
            }
        }
        $this->view->form = $form;
	}


	//VCT REPLY MESSAGE TO CUSTOMER
	public function replyMessageAction() {
		$rq   = $this->_request;

    	$tbSiteSetting = new Model_DbTable_SiteSetting();
        $siteSetting   = $tbSiteSetting->getSiteSetting()->query()->fetch();

        $form = new Admin_Form_AddSiteReplyMessage();
        if($rq->isPost()) {
            if($form->isValid($rq->getParams())) {
                $data = $form->getValues();
                if(!$siteSetting) {
                    $r = $tbSiteSetting->safelyInsert($data);
                } else {
                    $r = $tbSiteSetting->safelyUpdate($data, array('id =?' => $siteSetting['id']));
                }
                $this->_flashMessenger->addMessage(
                	$r
                    ? self::$msgUpdateSuccess
                    : self::$msgUpdateFailed);
                return $this->_redirect('/admin/site-setting/reply-message');
            }
        } else {
            if($siteSetting) {
                $form->populate($siteSetting);
            }
        }
        $this->view->form = $form;
	}
}
